home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 34.zip / BS1 part 34 / Aztec C 5.0a disk 3.adf / crt_src / cliparse.c < prev    next >
C/C++ Source or Header  |  1989-10-27  |  2KB  |  87 lines

  1. /* Copyright (C) 1986,1987 by Manx Software Systems, Inc. */
  2.  
  3. /*
  4.  *    This routine is called from the _main() routine and is used to
  5.  *    parse the arguments passed from the CLI to the program. It sets
  6.  *    up an array of pointers to arguments in the global variables and
  7.  *    and sets up _argc and _argv which will be passed by _main() to
  8.  *    the main() procedure. If no arguments are ever going to be
  9.  *    parsed, this routine may be replaced by a stub routine to reduce
  10.  *    program size.
  11.  *
  12.  *    If _arg_lin is non-zero, the _exit() routine will call FreeMem()
  13.  *    with _arg_lin as the memory to free and _arg_len as the size.
  14.  *
  15.  */
  16.  
  17. #include <libraries/dosextens.h>
  18. #include <string.h>
  19. #include <functions.h>
  20.  
  21. extern int _argc, _arg_len;
  22. extern char **_argv, *_arg_lin;
  23. extern char *_detach_name;            /* for DETACHED programs */
  24.  
  25. void
  26. _cli_parse(struct Process *pp, long alen, register char *aptr)
  27. {
  28.     register char *cp;
  29.     register struct CommandLineInterface *cli;
  30.     register int c;
  31.  
  32.     if (pp->pr_CLI) {
  33.         cli = (struct CommandLineInterface *) ((long)pp->pr_CLI << 2);
  34.         cp = (char *)((long)cli->cli_CommandName << 2);
  35.     }
  36.     else
  37.         cp = _detach_name;
  38.     _arg_len = cp[0]+alen+2;
  39.     if ((_arg_lin = AllocMem((long)_arg_len, 0L)) == 0)
  40.         return;
  41.     c = cp[0];
  42.     strncpy(_arg_lin, cp+1, (size_t)c);
  43.     strcpy(_arg_lin+c, " ");
  44.     strncat(_arg_lin, aptr, (size_t)alen);
  45.     _arg_lin[c] = 0;
  46.     for (_argc=1,aptr=cp=_arg_lin+c+1;;_argc++) {
  47.         while ((c=*cp) == ' ' || c == '\t' || c == '\f' ||
  48.                                                 c == '\r' || c == '\n')
  49.             cp++;
  50.         if (*cp < ' ')
  51.             break;
  52.         if (*cp == '"') {
  53.             cp++;
  54.             while (c = *cp++) {
  55.                 *aptr++ = c;
  56.                 if (c == '"') {
  57.                     if (*cp == '"')
  58.                         cp++;
  59.                     else {
  60.                         aptr[-1] = 0;
  61.                         break;
  62.                     }
  63.                 }
  64.             }
  65.         }
  66.         else {
  67.             while ((c=*cp++) && c != ' ' && c != '\t' && c != '\f' &&
  68.                                                 c != '\r' && c != '\n')
  69.                 *aptr++ = c;
  70.             *aptr++ = 0;
  71.         }
  72.         if (c == 0)
  73.             --cp;
  74.     }
  75.     *aptr = 0;
  76.     if ((_argv = AllocMem((long)(_argc+1)*sizeof(*_argv), 0L)) == 0) {
  77.         _argc = 0;
  78.         return;
  79.     }
  80.     for (c=0,cp=_arg_lin;c<_argc;c++) {
  81.         _argv[c] = cp;
  82.         cp += strlen(cp) + 1;
  83.     }
  84.     _argv[c] = 0;
  85. }
  86.  
  87.